package cn.innosmart.aq.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.AquariumBean;
import cn.innosmart.aq.util.SystemConstant;

/**
 * Created by David on 2015/7/24.
 */
public class AquariumListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private ArrayList<AquariumBean> aquariumBeans;
    private AquariumBean aquariumBean;
    private View.OnClickListener onClickListener;
    private boolean isModify = false;

    public AquariumListAdapter(Context context, View.OnClickListener onClickListener) {
        mInflater = LayoutInflater.from(context);
        mContext = context;
        aquariumBeans = SystemConstant.aquariumList;
        this.onClickListener = onClickListener;
    }

    @Override
    public int getCount() {
        if(aquariumBeans == null) {
            return 0;
        }
        return aquariumBeans.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return aquariumBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_aquariumlist_item, null);
            holder.llAquarium = (LinearLayout) convertView.findViewById(R.id.ll_aquarium);
            holder.rlAdd = (RelativeLayout) convertView.findViewById(R.id.rl_addlayout);
            holder.tv_aquarium_index = (TextView) convertView.findViewById(R.id.tv_aquarium_index);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tvWaterLeverFlag = (TextView) convertView.findViewById(R.id.tv_water_lever_flag);
            holder.tv_water_lever = (TextView) convertView.findViewById(R.id.tv_water_lever);
            holder.iv_alarm = (ImageView) convertView.findViewById(R.id.iv_alarm);
            holder.tv_temp = (TextView) convertView.findViewById(R.id.tv_temp);
            holder.tv_ph = (TextView) convertView.findViewById(R.id.tv_ph);
            holder.modify_setting = (ImageView) convertView.findViewById(R.id.modify_setting);
            holder.modify_delete = (ImageView) convertView.findViewById(R.id.modify_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == aquariumBeans.size()) {
            holder.llAquarium.setVisibility(View.GONE);
            holder.rlAdd.setVisibility(View.VISIBLE);
            holder.rlAdd.setOnClickListener(onClickListener);
        } else {
            holder.rlAdd.setVisibility(View.GONE);
            holder.llAquarium.setVisibility(View.VISIBLE);
            aquariumBean = SystemConstant.aquariumList.get(position);
            holder.llAquarium.setTag(position);
            holder.llAquarium.setOnClickListener(onClickListener);
            holder.tv_aquarium_index.setText(String.valueOf(position + 1));
            holder.tv_title.setText(aquariumBean.getTitle());
            String phValue = aquariumBean.getPhValue();
            if (phValue != null) {
                holder.tv_ph.setText(String.format(mContext.getString(R.string.aquariumlistadapter_ph), phValue));
            } else {
                if (aquariumBean.getPhs().size() > 0) {
                    holder.tv_ph.setText(mContext.getString(R.string.listview_aquariumvalue_unknown));
                } else {
                    holder.tv_ph.setText(mContext.getString(R.string.no_device_installed));
                }
            }
            String temValue = aquariumBean.getTemperatureValue();
            if (temValue != null) {
                holder.tv_temp.setText(temValue);
            } else {
                if (aquariumBean.getTemperatures().size() > 0) {
                    holder.tv_temp.setText(mContext.getString(R.string.listview_aquariumvalue_unknown));
                } else {
                    holder.tv_temp.setText(mContext.getString(R.string.no_device_installed));
                }
            }
            String waterLeverValue = aquariumBean.getWaterLeverValue(mContext);
            if (!TextUtils.isEmpty(waterLeverValue)) {
                holder.tv_water_lever.setText(waterLeverValue);
            } else {
                if (aquariumBean.getWaterLevers().size() > 0) {
                    holder.tv_water_lever.setText(mContext.getString(R.string.listview_aquariumvalue_unknown));
                } else {
                    holder.tv_water_lever.setText(mContext.getString(R.string.no_device_installed));
                }
            }
            //判断鱼缸是否存在异常
            if (aquariumBean.isAlarm()) {
                holder.llAquarium.setBackgroundResource(R.drawable.alarm_bg);
                holder.tv_water_lever.setTextColor(mContext.getResources().getColor(R.color.font_white));
                holder.tv_ph.setTextColor(mContext.getResources().getColor(R.color.font_white));
                holder.tv_title.setTextColor(mContext.getResources().getColor(R.color.font_white));
                holder.tvWaterLeverFlag.setTextColor(mContext.getResources().getColor(R.color.font_white));
                holder.tv_temp.setTextColor(mContext.getResources().getColor(R.color.font_white));
                holder.iv_alarm.setVisibility(View.VISIBLE);
            } else {
                holder.llAquarium.setBackgroundResource(R.drawable.list_background);
                holder.iv_alarm.setVisibility(View.GONE);
                holder.tv_temp.setTextColor(mContext.getResources().getColor(R.color.font_orange));
                holder.tv_ph.setTextColor(mContext.getResources().getColor(R.color.font_blue));
                holder.tvWaterLeverFlag.setTextColor(mContext.getResources().getColor(R.color.font_lightwhite));
                holder.tv_water_lever.setTextColor(mContext.getResources().getColor(R.color.font_lightwhite));
                holder.tv_title.setTextColor(mContext.getResources().getColor(R.color.font_lightwhite));
            }
            if (isModify) {
                holder.modify_setting.setVisibility(View.VISIBLE);
                holder.modify_delete.setVisibility(View.VISIBLE);
                holder.tv_temp.setVisibility(View.INVISIBLE);
                holder.tv_ph.setVisibility(View.INVISIBLE);
                holder.modify_setting.setTag(position);
                holder.modify_delete.setTag(position);
                holder.modify_delete.setOnClickListener(onClickListener);
                holder.modify_setting.setOnClickListener(onClickListener);
            } else {
                holder.tv_temp.setVisibility(View.VISIBLE);
                holder.tv_ph.setVisibility(View.VISIBLE);
                holder.modify_setting.setVisibility(View.INVISIBLE);
                holder.modify_delete.setVisibility(View.INVISIBLE);
            }
        }

        return convertView;
    }

    /**
     * 进入修改模式
     *
     * @param change
     */
    public void startModify(boolean change) {
        this.isModify = change;
        notifyDataSetChanged();
    }

    /**
     * 更新数据并通知UI发生变化
     */
    @Override
    public void notifyDataSetChanged() {
        aquariumBeans = SystemConstant.aquariumList;
        super.notifyDataSetChanged();
    }

    static class ViewHolder {
        TextView tv_aquarium_index;
        TextView tv_title;
        TextView tv_water_lever;
        ImageView iv_alarm;
        TextView tv_temp;
        TextView tv_ph;
        TextView tvWaterLeverFlag;
        ImageView modify_setting;
        ImageView modify_delete;
        LinearLayout llAquarium;
        RelativeLayout rlAdd;
    }
}
